package server

import (
	"context"
	"errors"
	"fmt"
	"time"
	"videos/video-rpc/initiate/mysql"
	"videos/video-rpc/initiate/redis"
	"videos/video-rpc/proto/video"
)

func UserRegister(in *video.UserRegisterReq) (*video.UserRegisterResp, error) {
	u := mysql.User{}
	redis.Client.Get(context.Background(), "SendRegister"+in.Tel)
	err := u.Find(in.Tel)
	if err != nil {
		fmt.Println("未查询到该用户")
		return nil, errors.New("未查询到该用户")
	}
	if u.Id != 0 {
		fmt.Println("该用户已经注册")
		return nil, errors.New("该用户已经注册")
	}
	err = u.Create()
	if err != nil {
		fmt.Println("用户注册失败")
		return nil, errors.New("用户注册失败")
	}
	redis.Client.Set(context.Background(), "SendRegister"+in.Tel, "1", time.Minute*5)
	redis.Client.Del(context.Background(), "SendRegister"+in.Tel)
	return &video.UserRegisterResp{Greet: "用户注册成功"}, nil
}

func UserLogin(in *video.UserLoginReq) (*video.UserLoginResp, error) {
	u := mysql.User{}
	redis.Client.Get(context.Background(), "SendLogin"+in.Tel)
	err := u.Find(in.Tel)
	if err != nil {
		fmt.Println("未查询到该用户")
		return nil, errors.New("未查询到该用户")
	}
	if u.Id == 0 {
		fmt.Println("该用户未注册")
		return nil, errors.New("该用户未注册")
	}
	if err != nil {
		fmt.Println("用户登录失败")
		return nil, errors.New("用户登录失败")
	}
	redis.Client.Set(context.Background(), "SendLogin"+in.Tel, "1", time.Minute*5)
	redis.Client.Del(context.Background(), "SendLogin"+in.Tel)
	return &video.UserLoginResp{Greet: "用户登录成功"}, nil
}

func VideoList(in *video.VideoListReq) (*video.VideoListResp, error) {
	w := mysql.VideoWorks{}
	li, err := w.List()
	if err != nil {
		fmt.Println("未展示出")
		return nil, err
	}
	var list []*video.VideoItem
	for _, l := range li {
		lists := &video.VideoItem{
			Title:          l.Title,
			Desc:           l.Desc,
			MusicId:        l.MusicId,
			WorkType:       l.WorkType,
			CheckStatus:    l.CheckStatus,
			IpAddress:      l.IpAddress,
			WorkPermission: l.WorkPermission,
			LikeCount:      l.LikeCount,
			CommentCount:   l.CommentCount,
		}
		list = append(list, lists)
	}
	return &video.VideoListResp{List: list}, nil
}

func VideoCreate(in *video.VideoCreateReq) (*video.VideoCreateResp, error) {
	w := mysql.VideoWorks{}
	err := w.Create()
	if err != nil {
		fmt.Println("上传失败")
		return nil, err
	}
	return &video.VideoCreateResp{Greet: "上传成功"}, nil
}
func VideoSO(in *video.VideoSOReq) (*video.VideoListResp, error) {
	w := mysql.VideoWorks{}
	li, err := w.Find(in.Title)
	if err != nil {
		fmt.Println("未展示出")
		return nil, err
	}
	var list []*video.VideoItem
	for _, l := range li {
		lists := &video.VideoItem{
			Title:          l.Title,
			Desc:           l.Desc,
			MusicId:        l.MusicId,
			WorkType:       l.WorkType,
			CheckStatus:    l.CheckStatus,
			IpAddress:      l.IpAddress,
			WorkPermission: l.WorkPermission,
			LikeCount:      l.LikeCount,
			CommentCount:   l.CommentCount,
		}
		list = append(list, lists)
	}
	return &video.VideoListResp{List: list}, nil
}

func VideoUpdate(in *video.VideoUpdateReq) (*video.VideoUpdateResp, error) {
	w := mysql.VideoWorks{}
	err := w.Finds(in.CheckStatus)
	if err != nil {
		fmt.Println("没有此作品")
		return nil, err
	}
	err = w.Update(in.CheckStatus)
	if err != nil {
		fmt.Println("修改失败")
		return nil, err
	}
	return &video.VideoUpdateResp{Success: true}, nil
}

func VideoDel(in *video.VideoDelReq) (*video.VideoDelResp, error) {
	w := mysql.VideoWorks{}
	err := w.GetFindById(in.Id)
	if err != nil {
		fmt.Println("没有此作品")
		return nil, err
	}
	err = w.GetDel(in.Id)
	if err != nil {
		fmt.Println("删除失败")
		return nil, err
	}
	return &video.VideoDelResp{Success: true}, nil
}
